package com.fhz.mvvmshopjava.utils;

import android.content.Context;

import com.fhz.mvvmshopjava.data.model.LoggedInUser;

import javax.inject.Inject;
import javax.inject.Singleton;

import dagger.hilt.android.qualifiers.ApplicationContext;

/**
 * 时间:2022/6/9
 *
 * @author msfeng
 * 简述: 用户信息的管理
 */

public class UserManager {

    private static final String USER = "user";

    private ACache mCache;

    @Inject
    public UserManager(Context context) {
        mCache = ACache.get(context);
    }

    private static UserManager userManager;

    public static UserManager getInstance(Context context){
        if(userManager == null){
            synchronized (UserManager.class){
                userManager = new UserManager(context);
            }
        }
        return userManager;
    }


    public void saveUser(LoggedInUser user){
        mCache.put(USER,user);
    }

    public LoggedInUser getUser(){
       return (LoggedInUser) mCache.getAsObject(USER);
    }

    public int getUserId(){
        if(getUser() != null){
            return getUser().getUserId();
        }
        return Constant.NO_LOGGED;
    }

    public String getString(String key){
        return mCache.getAsString(key);
    }

    public void putString(String key,String value){
         mCache.put(key,value);
    }






}
